package com.room.search.service;

import com.room.search.core.page.TableDataInfo;
import com.room.search.domain.RoomBroker;
import com.room.search.repository.RoomBrokerRepository;
import com.room.search.response.RoomResult;
import com.room.search.service.dto.RoomBrokerDTO;
import com.room.search.service.mapper.RoomBrokerMapper;
import java.util.LinkedList;
import java.util.List;
import java.util.Optional;
import java.util.stream.Collectors;

import com.room.search.util.PageUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

/**
 * Service Implementation for managing {@link RoomBroker}.
 */
@Service
@Transactional
public class RoomBrokerService {

    private final Logger log = LoggerFactory.getLogger(RoomBrokerService.class);

    private final RoomBrokerRepository roomBrokerRepository;

    private final RoomBrokerMapper roomBrokerMapper;

    public RoomBrokerService(RoomBrokerRepository roomBrokerRepository, RoomBrokerMapper roomBrokerMapper) {
        this.roomBrokerRepository = roomBrokerRepository;
        this.roomBrokerMapper = roomBrokerMapper;
    }

    /**
     * Save a roomBroker.
     *
     * @param roomBrokerDTO the entity to save.
     * @return the persisted entity.
     */
    public RoomBrokerDTO save(RoomBrokerDTO roomBrokerDTO) {
        log.debug("Request to save RoomBroker : {}", roomBrokerDTO);
        RoomBroker roomBroker = roomBrokerMapper.toEntity(roomBrokerDTO);
        roomBroker = roomBrokerRepository.save(roomBroker);
        return roomBrokerMapper.toDto(roomBroker);
    }

    /**
     * Partially update a roomBroker.
     *
     * @param roomBrokerDTO the entity to update partially.
     * @return the persisted entity.
     */
    public Optional<RoomBrokerDTO> partialUpdate(RoomBrokerDTO roomBrokerDTO) {
        log.debug("Request to partially update RoomBroker : {}", roomBrokerDTO);

        return roomBrokerRepository
            .findById(roomBrokerDTO.getId())
            .map(existingRoomBroker -> {
                roomBrokerMapper.partialUpdate(existingRoomBroker, roomBrokerDTO);

                return existingRoomBroker;
            })
            .map(roomBrokerRepository::save)
            .map(roomBrokerMapper::toDto);
    }

    /**
     * Get all the roomBrokers.
     *
     * @return the list of entities.
     */
    @Transactional(readOnly = true)
    public List<RoomBrokerDTO> findAll() {
        log.debug("Request to get all RoomBrokers");
        return roomBrokerRepository.findAll().stream().map(roomBrokerMapper::toDto).collect(Collectors.toCollection(LinkedList::new));
    }

    /**
     * Get one roomBroker by id.
     *
     * @param id the id of the entity.
     * @return the entity.
     */
    @Transactional(readOnly = true)
    public Optional<RoomBrokerDTO> findOne(Long id) {
        log.debug("Request to get RoomBroker : {}", id);
        return roomBrokerRepository.findById(id).map(roomBrokerMapper::toDto);
    }

    /**
     * Delete the roomBroker by id.
     *
     * @param id the id of the entity.
     */
    public void delete(Long id) {
        log.debug("Request to delete RoomBroker : {}", id);
        roomBrokerRepository.deleteById(id);
    }

    /**
     * 获取所有的房产经纪人
     */
    public TableDataInfo getAllBroker(RoomBrokerDTO dto) {
        Pageable pageable = PageUtils.startPage();
        Page<RoomBroker> all = roomBrokerRepository.findAll(pageable);
        return TableDataInfo.getTableDataInfo(all);

    }
}
